package com.deli.springboot.testhttpclient;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;


public class HttpClient{
    private static Logger log = LoggerFactory.getLogger(HttpClient.class);
    /**
     * 功能： 向呼叫控制发送呼叫信息
     * @param encode
     * @param sparameters POST 的参数
     * @param curl 呼叫控制的链接
     * @return*/
    public static String sendPostRequest(String encode,String sparameters, String curl){
        String strRtn = "-1";
        URL url = null;
        String str = null;
        OutputStreamWriter os = null;
        BufferedReader is = null;
        HttpURLConnection httpConn = null;
        try{
            url = new URL(curl);
            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setRequestMethod("POST");
            os = new OutputStreamWriter(httpConn.getOutputStream(), encode);
            os.write(sparameters);
            os.flush();
            os.close();
            is = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            while ((str = is.readLine()) != null){
                strRtn = str;
            }
            is.close();
            os = null;
            is = null;
            str = null;
            httpConn.disconnect();
        }
        catch (MalformedURLException e){
            strRtn = "malError";
            e.printStackTrace();
        }
        catch (UnknownHostException unhostex){
            log.debug("in requestUrl() UnknownHostException error: "+ unhostex.getMessage());
            strRtn = "unknowError";
        }
        catch (IOException ex){
            log.debug("in requestUrl() error: " + ex.toString());
            strRtn = "ioError";
        }
        finally{
            try{
                if (os != null)
                    os.close();
                if (is != null)
                    is.close();
            }
            catch (IOException e){
                e.printStackTrace();
            }
        }
        log.debug("strRtn is [" + strRtn + "]");
        return strRtn;
    }
    /**
     * @param encode
     * @param sparameters
     * @param curl
     * @return
     */
    public static String sendGetRequest(String encode,String sparameters, String curl){
        String strRtn = "-1";
        URL url = null;
        String str = null;
        BufferedReader is = null;
        HttpURLConnection httpConn = null;
        StringBuffer sb = null;
        try{
            sb = new StringBuffer(curl);
            sb.append("?");
            sb.append(sparameters);
            url = new URL(sb.toString());
            httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            httpConn.setRequestMethod("GET");
            is = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            while ((str = is.readLine()) != null){
                strRtn = str;
            }
            is.close();
            is = null;
            str = null;
            httpConn.disconnect();
        }
        catch (MalformedURLException e){
            strRtn = "malError";
            e.printStackTrace();
        }
        catch (UnknownHostException unhostex){
            log.debug("in requestUrl() UnknownHostException error: "+ unhostex.getMessage());
            strRtn = "unknowError";
        }
        catch (IOException ex){
            log.debug("in requestUrl() error: " + ex.toString());
            strRtn = "ioError";
        }
        finally{
            try
            {
                if (is != null)
                    is.close();
            }
            catch (IOException e){
                e.printStackTrace();
            }
        }
        log.debug("strRtn is [" + strRtn + "]");
        return strRtn;
    }
}